package edu.pku.hawk.controller.detectmonitor;

import edu.pku.hawk.controller.util.Constants;
import edu.pku.hawk.controller.util.MConsumerQueue;
import edu.pku.hawk.controller.util.MProducerQueue;
import edu.pku.hawk.controller.util.MQManager;

public class BaseDetectionMonitor {

	private MProducerQueue toClientQueue;
	private MConsumerQueue fromRowQueue;

	public BaseDetectionMonitor(String toClientQueueName) throws Exception {
		MQManager mqmgr = new MQManager(Constants.MQServer_Name, Constants.MQServer_Password,
				Constants.MQServer_URL);
		toClientQueue = mqmgr.getProducerQueue(toClientQueueName);
	}

	public void run() {
		//
		while (true) {

			// Fetch detector data from DB and analyze it.
			AnalyzeResult rslt = analysis();

			// Send single analyzeResult to client
			sendToClient(rslt);

			// Send analyzeResult to process controller
			sendToProcessController(rslt);
		}
	}

	public AnalyzeResult analysis() {
		return null;
	}

	public void sendToClient(AnalyzeResult rslt) {
		try {
			toClientQueue.send(rslt.getDtctRprt());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void sendToProcessController(AnalyzeResult rslt) {
		return;
	}

	public static void main(String[] args) {
		try {
			BaseDetectionMonitor bdm = new SmokeDetectionMonitor(Constants.Ctrl_UI_Path_QueueName);
			bdm.run();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
